/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlador;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;

/**
 *
 * @author Windows7
 */
public class RegistroControl extends HttpServlet {

  Connection conn = null;

  public void init (ServletConfig config) throws ServletException {

    super.init(config);
    String url="jdbc:mysql://localhost:3306/hms";
      //connection=DriverManager.getConnection(url,"postgres","postgres");
    try {
      Class.forName("com.mysql.jdbc.Driver");
    } catch(ClassNotFoundException ex) {
      System.out.println("Error al cargar el driver");
      System.out.println(ex.getMessage());
    }
    try {
      conn=DriverManager.getConnection(url,"root","mysql");
    } catch (SQLException sqlEx) {
      System.out.println("Se ha producido un error al establecer la conexion con: "+url);
      System.out.println(sqlEx.getMessage());
    }
  }

  public void destroy () {
    super.destroy();
    System.out.println("Cerrando conexion...");
    try {
      conn.close();
    } catch(SQLException ex){
      System.out.println("No se pudo cerrar la conexion");
      System.out.println(ex.getMessage());
    }
  }

  public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    Statement stmt = null;
    ResultSet rs = null;

    String dni = null;
    dni  = req.getParameter("dni");

    try {
      stmt=conn.createStatement();
      rs = stmt.executeQuery("SELECT * FROM `hms`.`paciente` WHERE PacDni ='" + dni + "'");
      if(rs.next()==true){
        DevolverHTML(req,resp,true);
      }else{
          
        stmt.executeUpdate("INSERT INTO `hms`.`paciente`(`PacNom`,`PacApe`,`PacDni`,`PacFecNac`,`PacSex`,`PacDir`,`PacTel`,`PacTipSan`)"
                + "VALUES('" + req.getParameter("nombre") + "','" + req.getParameter("apellido")+"','" 
                + req.getParameter("dni") + "','" + req.getParameter("fecnac") 
                + "','" + req.getParameter("sex") + "','" + req.getParameter("direc") 
                + "','" + req.getParameter("telefono") + "','" + req.getParameter("tiposan") + "')");
           
        DevolverHTML(req,resp,false);
      }
      return;      
    } catch (SQLException sql) {
      System.out.println("Se produjo un error al crear el Statement");
      System.out.println(sql.getMessage());
      System.out.println(sql);
      return;
    } finally {
      if(stmt!=null) {
        try {
          stmt.close();
        } catch(SQLException e) {
          System.out.println("Error al cerrar el Statement");
          System.out.println(e.getMessage());
          return;
        }
      }
    }
  }

  public void DevolverHTML(HttpServletRequest req, HttpServletResponse resp, boolean existe) throws SQLException {

    resp.setContentType("text/html");
    PrintWriter out=null;
    try {
      out=resp.getWriter();
    } catch (IOException io) {
      System.out.println("Se ha producido una excepcion");
    }
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Hospital Management System</title>");
    out.println("</head>");
    out.println("<body>");
    if (existe==false){
      out.println("<H2 ALIGN=\"center\">Se ha registrado un nuevo paciente.</H2></P>");
    }else{
      out.println("<H2 ALIGN=\"center\">El paciente ya existe.<H2></P>");
    }
    out.println("</body>");
    out.println("</html>");
    out.flush();
    out.close();
  }
}
